package com.example.tourpal;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridLayout;
import android.widget.TextView;

public class ShareAppActivity extends Activity {

	// UI
	private GridLayout shareAppGrid;
	private TextView cancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.share_app_view);

		setupView();
	}

	private void setupView() {

		shareAppGrid = (GridLayout) findViewById(R.id.shareAppGrid);

		cancel = (TextView) findViewById(R.id.cancel);
		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		getShareApps(this);
	}

	public void getShareApps(Context context) {
		List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
		Intent intent = new Intent(Intent.ACTION_SEND, null);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setType("image/jpeg");
		PackageManager pManager = context.getPackageManager();
		mApps = pManager.queryIntentActivities(intent,
				PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);

		for (int i = 0; i < mApps.size(); i++) {
			final ResolveInfo appInfo = mApps.get(i);

			String name = appInfo.activityInfo.name;
			Drawable drawable = appInfo.loadIcon(pManager);
			if (name.indexOf("mms.ui.ComposeMessageActivity") > 0) {
				drawable = getResources().getDrawable(R.drawable.share_message);
				name = getResources().getString(R.string.share_mms_name);
			} else if (name.indexOf("sina.weibo.EditActivity") > 0) {
				drawable = getResources().getDrawable(R.drawable.share_weibo);
				name = getResources().getString(R.string.share_sina_weibo_name);
			} else if (name.indexOf("ui.tools.ShareImgUI") > 0) {
				drawable = getResources().getDrawable(R.drawable.share_weixin);
				name = getResources().getString(R.string.share_weixin_name);
			} else if (name.indexOf("ui.tools.ShareToTimeLineUI") > 0) {
				drawable = getResources().getDrawable(
						R.drawable.share_pengyouquan);
				name = getResources().getString(
						R.string.share_weixin_friends_name);
			} else {
				//continue;
				name = appInfo.loadLabel(pManager).toString();
			}

			TextView shareIcon = new TextView(context);
			shareIcon.setGravity(Gravity.CENTER);
			shareIcon.setText(name);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(),
					drawable.getMinimumHeight());
			shareIcon.setCompoundDrawables(null, drawable, null, null);
			shareIcon.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					File file = new File(
							"storage/sdcard0/Pictures/planCard.jpg");
					Intent shareIntent = new Intent(Intent.ACTION_SEND);
					shareIntent.setComponent(new ComponentName(
							appInfo.activityInfo.packageName,
							appInfo.activityInfo.name));
					shareIntent.putExtra(Intent.EXTRA_STREAM,
							Uri.fromFile(file));
					shareIntent.setType("image/jpeg");
					startActivity(shareIntent);
				}
			});

			GridLayout.LayoutParams iconParams = new GridLayout.LayoutParams();
			iconParams.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
			iconParams.setMargins(16, 16, 16, 16);
			shareAppGrid.addView(shareIcon, iconParams);
		}

	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		overridePendingTransition(R.anim.main_enter, R.anim.sub_exit);
	}
}
